package edu.kit.ksri.db.mturk.utils.messaging;

import java.util.ArrayList;
import java.util.List;

import com.amazonaws.mturk.requester.Assignment;

import edu.kit.ksri.db.mturk.plugins.display.BrowserDisplay;
import edu.kit.ksri.db.mturk.var.StaticInformation;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 */
public class ResultQueue {

	/** The queue object */
	private final List<Result> q = new ArrayList<Result>();

	/** the {@link ResultQueue} singleton */
	private static ResultQueue instance;

	private ResultQueue() {}
	
	/**
	 * 
	 * @return the {@link ResultQueue} singleton
	 */
	public static ResultQueue getInstance() {
		if (instance == null)
			instance = new ResultQueue();
		return instance;
	}

	/**
	 * enqueues a {@link Result} object
	 * 
	 * @param result
	 */
	public void enqueue(Result result) {
		synchronized (q) {
			q.add(result);
		}
		
		if(StaticInformation.isMonitoring)
			BrowserDisplay.update(result);
	}

	/**
	 * convenience method. Creates a {@link Result} object and enqueues it.
	 * 
	 * @see Result
	 * 
	 * @param hitId
	 * @param assignments
	 */
	public void enqueue(String hitId, Assignment[] assignments) {
		this.enqueue(new Result(hitId, assignments));
	}

	/**
	 * convenience method. Creates a {@link Result} object and enqueues it.
	 * 
	 * @see Result
	 * 
	 * @param hitId
	 * @param assignments
	 */
	public void enqueue(String hitId, List<Assignment> assignments) {
		this.enqueue(new Result(hitId, assignments));
	}

	/**
	 * 
	 * @return the number of elements within this queue
	 */
	public int getSize() {
		synchronized (q) {
			return q.size();
		}
	}

	/**
	 * 
	 * @return the first element from the queue. The element is also
	 *         <b>removed</b> from the queue. If no entries exist the method
	 *         returns null.
	 */
	public Result getResult() {
		synchronized (q) {
			if (q.size() == 0)
				return null;
			return q.remove(0);
		}
	}

	/**
	 * 
	 * @return all entries from the queue or an empty {@link List} if the queue
	 *         was empty. The order of the elements is not affected.
	 */
	public List<Result> getAllResults() {
		ArrayList<Result> res = new ArrayList<Result>();
		synchronized (q) {
			if (q.size() == 0)
				return res;
			res.addAll(q);
			q.clear();
		}
		return res;

	}
}
